from flask import Blueprint,request,jsonify
from utils.db import db
from utils.comm import get_data,xtree

import json

bp_course = Blueprint('course',__name__)


#无限级分类
@bp_course.route('/cates/',methods=["POST","GET","DELETE"])
def cates():
    if request.method =="POST":
        data = get_data()
        sql = "insert into cates values(0,'%s',%d,%s) "%(data['name'],int(data['pid']),data['params'])
        print(sql)
        db.insert(sql)
        return jsonify({"code":200})

    elif request.method =="GET":
        #获取全部数据
        sql = "select * from cates"
        res = db.find_all(sql)
        print(res)
        
        list = xtree(res)
        return jsonify({'code':200,'list':list})
    else:
        pass

@bp_course.route('/pageshow/',methods=["POST","GET","DELETE"])
def pageshow():
    if request.method =="POST":
        pass
    elif request.method =="GET":
        pagesize = 2
        page = request.args.get('page')
        start = (int(page)-1)*pagesize

        sql = "select * from cates  order by id asc limit %d,%d"%(start,pagesize)
        res = db.find_all(sql)
        sql1 = "select count(id) as total from cates"
        res1 = db.find_one(sql1)
        print(res)
        return jsonify({"code":200,'list':res,'total':res1['total']})
    else:
        pass